package com.shop.admin.service.impl;

import com.shop.Result.LdbzResult;
import com.shop.admin.service.AdminAuthService;
import com.shop.dao.LdbzAdminAuthMapper;
import com.shop.pojo.LdbzAdminAuth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminAuthServiceImpl implements AdminAuthService {

    private static final Logger logger = LoggerFactory.getLogger(AdminAuthServiceImpl.class);

    @Autowired
    private LdbzAdminAuthMapper ldbzAdminAuthMapper;


    @Override
    public LdbzResult selectUserByRole(Long roleId, int pn, int limit, String realName) {
        Map<String,Object> map = new HashMap();
        Long total = ldbzAdminAuthMapper.countUserByRole(roleId, realName);
        map.put("total",total);
        logger.debug("selectUserByRole count : {}",total);
        if (total > 0 && pn > 0){
            int start = (pn - 1) * limit;
            List<LdbzAdminAuth> ldbzAdminAuths = ldbzAdminAuthMapper.selectUserByRole(roleId, start, limit, realName);
            map.put("list",ldbzAdminAuths);
        }
        return LdbzResult.build(0,"",map);
    }

    @Override
    public LdbzResult selectMenuByRole(Long roleId, int pn, int limit, String menuName) {
        Map<String,Object> map = new HashMap();
        Long total = ldbzAdminAuthMapper.countMenuByRole(roleId, menuName);
        map.put("total",total);
        logger.debug("selectMenuByRole count : {}",total);
        if (total > 0 && pn > 0){
            int start = (pn - 1) * limit;
            List<LdbzAdminAuth> ldbzAdminAuths = ldbzAdminAuthMapper.selectMenuByRole(roleId, start, limit, menuName);
            map.put("list",ldbzAdminAuths);
        }
        return LdbzResult.build(0,"",map);
    }

    @Override
    public LdbzResult deleteByUser(Long userId, Long roleId) {
        logger.debug("deleteByUser userId : {}",userId);
        return LdbzResult.ok(ldbzAdminAuthMapper.deleteByUser(userId,roleId));
    }

    @Override
    public LdbzResult deleteByMenu(Long menuId, Long roleId) {
        logger.debug("deleteByMenu menuId : {} ",menuId);
        return LdbzResult.ok(ldbzAdminAuthMapper.deleteByMenu(menuId, roleId));
    }

    @Override
    public LdbzResult insertByMenu(LdbzAdminAuth entity) {
        logger.debug("insertByMenu entity : {} ",entity);
        if (ldbzAdminAuthMapper.insertByMenu(entity)>=1) {
            return LdbzResult.ok();
        } else {
            return LdbzResult.build(500,"添加失败");
        }
    }

    @Override
    public LdbzResult insertByUser(LdbzAdminAuth entity) {
        logger.debug("insertByUser entity : {} ",entity);
        if (ldbzAdminAuthMapper.insertByUser(entity)>=1) {
            return LdbzResult.ok();
        } else {
            return LdbzResult.build(500,"添加失败");
        }
    }

    @Override
    public LdbzResult getUserByNameOrAccount(String queryKey) {
        logger.debug("getUserByNameOrAccount : {} ",queryKey);
        return LdbzResult.ok(ldbzAdminAuthMapper.getUserByNameOrAccount(queryKey));
    }

    @Override
    public LdbzResult getResByNameOrUrl(String queryKey) {
        logger.debug("getResByNameOrUrl : {} ",queryKey);
        return LdbzResult.ok(ldbzAdminAuthMapper.getResByNameOrUrl(queryKey));
    }
}
